/*
 * @Author: chunlai
 * @Date: 2025-07-09 09:47:45
 * @LastEditTime: 2025-11-27 13:36:32
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \01.latest-short-insure\src\store\modules\json.js
 * @Description: json配置请求接口
 */
import $http from "@/utils/request.js";
const version = require("@/config/version.json");
import { getDataFromUrl } from "@/utils/index";
const apiDomain = `${process.env.VUE_APP_PATH}`,
  ver_num = `${JSON_VERSION || version.version}`;

const state = {
  /**是否已获取配置，防止页面刷新二次请求接口*/
  dictConfigsJson: -1,
  pageConfigsJson: -1,
  noticeConfigsJson: -1,
  insuConfigsJson: -1,
};
const getters = {};
const mutations = {
  SET_STATE_KEY(state, key) {
    state[key] = 1;
  },
};
const getReqUrl = (pname) => {
  const gysName = getDataFromUrl("company");
  return `${apiDomain}dataModules/${gysName}/${pname}.json?v=${ver_num}`;
};

const isObject = (value) => {
  return Object.prototype.toString.call(value) === "[object Object]";
};
const resetAreaCodes = (list) => {
  list.forEach((item) => {
    item.defaultIndex = 0;
    if (item.children) {
      if (item.children.length == 0) {
        delete item.children;
      } else {
        resetAreaCodes(item.children);
      }
    }
  });
  return list;
};
// ,{ root: true }
const actions = {
  resetApp({ commit, state }) {
    return new Promise((resolve, reject) => {
      commit("order/setOrderNum", "", {
        root: true,
      });
      commit("agent/SET_STAFF_API_REQ", -1, {
        root: true,
      });
      commit("app/RESET_URL_KEYS", "", {
        root: true,
      });
      Object.keys(state).map((key) => {
        state[key] = -1;
      });
      setTimeout(() => {
        resolve();
      }, 30);
    });
  },
  // 证件有效期填写说明提示文档
  getInsureJson({ commit, state }) {
    return new Promise((resolve, reject) => {
      if (state.insuConfigsJson === 1) return resolve("1");
      $http
        .get(`${apiDomain}dataModules/insure.json?v=${ver_num}`, {
          baseURL: "",
        })
        .then((res) => {
          commit("page/SET_INSURE_CONFIG", {}, { root: true });
          commit("SET_STATE_KEY", "insuConfigsJson");
          if (isObject(res) && Object.keys(res).length > 0) {
            commit("page/SET_INSURE_CONFIG", res, { root: true });
          }
          resolve("1");
        });
    });
  },
  getHealthyJson({ commit, rootState, rootGetters }, jname = "") {
    return new Promise((resolve, reject) => {
      commit("updateLoad", true, { root: true });
      let jsonName = "healthy";
      if (jname) {
        jsonName = jname;
      } else {
        const nameList = rootGetters["notice/nameList"],
          proIdCode = rootState.proIdCode,
          mainRiskCode = rootState.mainRiskCode,
          proUniCode = proIdCode || mainRiskCode;
        if (nameList.length > 0) {
          const fdObj = nameList.find((e) => e.proCodes.includes(proUniCode));
          if (fdObj && fdObj.name) {
            jsonName = `${fdObj.name}`;
          }
        }
      }
      $http
        .get(getReqUrl(jsonName), {
          baseURL: "",
        })
        .then((res) => {
          commit("updateLoad", false, { root: true });
          resolve(res);
        })
        .finally(() => {
          commit("updateLoad", false, {
            root: true,
          });
        });
    });
  },
  getBaseDataJson({ state, commit, dispatch }, jsonName) {
    console.log("getBaseDataJson-->", jsonName);
    return new Promise((resolve, reject) => {
      if (["page"].includes(jsonName)) {
        if (state.pageConfigsJson === 1) return resolve("1");
      } else if (["notice"].includes(jsonName)) {
        if (state.noticeConfigsJson === 1) return resolve("1");
      } else if (["dictionary"].includes(jsonName)) {
        if (state.dictConfigsJson === 1) return resolve("1");
      } else {
        commit("updateLoad", true, { root: true });
      }
      $http
        .get(getReqUrl(jsonName), {
          baseURL: "",
        })
        .then((res) => {
          let data = [];
          if (jsonName == "area") {
            data = resetAreaCodes(res.addressData);
          } else if (jsonName == "workList") {
            data = res[0].child;
          } else if (jsonName == "notice") {
            commit("SET_STATE_KEY", "noticeConfigsJson");
            if (isObject(res) && Object.keys(res).length > 0) {
              commit("notice/SET_STATE_FIELD", res, {
                root: true,
              });
            }
          } else if (jsonName == "page") {
            commit("SET_STATE_KEY", "pageConfigsJson");
            if (isObject(res) && Object.keys(res).length > 0) {
              if (res.inputReg) {
                commit(
                  "infoReg/SET_INFO_DATA",
                  {
                    inputReg: res.inputReg,
                  },
                  {
                    root: true,
                  }
                );
                delete res.inputReg;
              }
              dispatch("page/initPageConfig", res, {
                root: true,
              });
            }
          } else if (jsonName == "dictionary") {
            commit("SET_STATE_KEY", "dictConfigsJson");
            if (process.env.VUE_APP_PRODFLAG !== "wxou") {
              commit(
                "dictionary/INIT_DATA",
                {
                  noneOcrCheckIdCard: true,
                },
                { root: true }
              );
            }
            if (isObject(res) && Object.keys(res).length > 0) {
              // 非正式环境,证件照不OCR识别
              dispatch("dictionary/initDictionaryCode", res, { root: true });
            }
          } else {
            data = res;
          }
          resolve(data);
          commit("updateLoad", false, { root: true });
        })
        .finally((err) => {
          commit("updateLoad", false, {
            root: true,
          });
        });
    });
  },
  getJson9Config({ commit, dispatch }, jsonName) {
    return new Promise((resolve, reject) => {
      $http
        .get(
          `//wxou.mschn.cn/app_h5/insurepics/jsons/${jsonName}.json?v=${ver_num}`,
          {
            baseURL: "",
          }
        )
        .then((res) => {
          resolve(res);
        });
    });
  },
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
